import {
  benchArticle, benchComment,
  getbenchCommentDetails, getbenchCommentReply,
  getbenchCommentDelete, getArticleDetails,
  getArticleDetailsDelete, getArticleDetailsDraft
} from '@/service';
import { IBencharticle, IBenchComment } from '@/types';
import { Effect, Reducer } from 'umi';
import { message } from 'antd'
export interface BenchModelState {
  benchArticleList: IBencharticle[];
  benchCommentList: IBenchComment[];
  getArticleDetailsList: any
}

export interface BenchModelType {
  namespace: 'bench';
  state: BenchModelState;
  effects: {
    getBenchArticle: Effect;
    getBenchComment: Effect;
    getbenchCommentDetails: Effect;
    getbenchCommentReply: Effect;
    getbenchCommentDelete: Effect;
    getArticleDetails: Effect;
    getArticleDetailsDelete: Effect;
    getArticleDetailsDraft: Effect
  };
  reducers: {
    save: Reducer<BenchModelState>;
  };
}


const SettingModel: BenchModelType = {
  namespace: 'bench',

  state: {
    benchArticleList: [],
    benchCommentList: [],
    getArticleDetailsList: {}
  },

  effects: {
    // 文章数据
    *getBenchArticle({ payload }, { call, put }) {
      let result = yield call(benchArticle);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            benchArticleList: result.data[0]
          }
        })
      }
    },
    // 评论数据
    *getBenchComment({ payload }, { call, put }) {
      let result = yield call(benchComment);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            benchCommentList: result.data[0]
          }
        })
      }
    },
    // 通过拒绝
    *getbenchCommentDetails({ payload }, { call, put }) {
      let result = yield call(getbenchCommentDetails, payload)
      if (result) {
        yield put({
          type: 'save',
          // payload: {
          //   benchCommentListdetails: result.date,
          // }
        })
      }
    },
    // 回复
    *getbenchCommentReply({ payload }, { call, put }) {
      let result = yield getbenchCommentReply(payload)
      if (result) {
        yield put({
          type: 'save',
        })
      }
    },
    // 删除
    *getbenchCommentDelete({ payload }, { call, put }) {
      let result = yield call(getbenchCommentDelete, payload)
      if (result) {
        yield put({
          type: 'save',
        })
      }
      if (result.success) {
        message.success('评论删除成功')
      } else {
        message.success('评论删除失败')
      }
    },
    // 详情编辑数据
    *getArticleDetails({ payload }, { call, put }) {
      let result = yield call(getArticleDetails, payload)
      localStorage.setItem('DetailsList', result.data.toc)
      if (result) {
        yield put({
          type: 'save',
          payload: {
            getArticleDetailsList: result.data
          }
        })
      }
    },
    // 详情编辑数据删除
    *getArticleDetailsDelete({ payload }, { call, put }) {
      let result = yield call(getArticleDetailsDelete, payload)
      console.log(result);

      console.log(result);
      if (result) {
        yield put({
          type: 'save',
        })
      }
    },
    // 草稿
    *getArticleDetailsDraft({ payload }, { call, put }) {
      let result = yield call(getArticleDetailsDraft, payload)
      console.log(result);
      console.log(result);
      if (result) {
        yield put({
          type: 'save',
        })
      }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};


export default SettingModel;
